home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CU Amiga Super CD-ROM 11
/
CU Amiga Magazine's Super CD-ROM 11 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-06].iso
/
www
/
http
/
www.amigasupport.com
/
software
/
arc
/
pngdt.lha
/
PNG_dt
/
ViewDT
/
ViewDT.c
< prev
Wrap
C/C++ Source or Header
|
1995-07-27
|
12KB
|
403 lines
/*
ViewDT
Simple DataType-based Picture Viewer
Syntax:
ViewDT FILES/A
Examples:
ViewDT pictures:space/#?.pic
ViewDT portrait.png
Source Code Version:
$VER: ViewDT.c 43.0
Status:
Public Domain
If you require more information please send E-mail to <info@cloanto.it>
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
#include <graphics/displayinfo.h>
#include <intuition/intuitionbase.h>
#include <intuition/gadgetclass.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/pictureclass.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/datatypes.h>
#include <string.h>
#include <stdio.h>
struct Picture
{
struct BitMapHeader bmhd; /* format and infos */
struct BitMap *bmap; /* bitmap */
ULONG *palette; /* color table in LoadRGB32() format */
LONG palette_size; /* mem usage */
LONG palette_entries; /* number of colors */
ULONG display_ID; /* video mode */
UBYTE *author; /* author info */
UBYTE *copyright; /* copyright info */
UBYTE *annotation; /* other info */
LONG author_size; /* mem usage */
LONG copyright_size; /* mem usage */
LONG annotation_size; /* mem usage */
};
void FreePicture(struct Picture *pic);
LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG);
BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size);
BOOL ViewPicture(struct Picture *pic);
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *DataTypesBase;
/*
IsDataTypes
Parameters
file_name: name of the file to inspect
name_buff: (optional) buffer to store the file format name
nbuff_size: size of name_buff
Return value
TRUE if DataTypes recognized the file as a valid picture file
FALSE otherwise
*/
BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
{
struct DataType *dtn;
struct DataTypeHeader *dth;
BPTR lock;
BOOL it_is;
it_is = FALSE;
if (lock = Lock(file_name, ACCESS_READ))
{
/* inspect file */
if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
{
dth = dtn->dtn_Header;
if (dth->dth_GroupID == GID_PICTURE) /* is it a picture? */
{
it_is = TRUE;
if (name_buff)
{
strncpy(name_buff, dth->dth_Name, nbuff_size);
*(name_buff + nbuff_size - 1) = 0; /* safe strncpy() termination */
}
}
ReleaseDataType(dtn);
}
UnLock(lock);
}
return(it_is);
}
/*
GetDataTypesPicture
Parameters
file_name: name of the file to load
pic: work structure
bmap_flags: AllocBitMap() flags (BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
Return value
0 if successful (picture info and data in "pic" structure) or
error code as from dos.library IoErr() function
*/
LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags)
{
Object *obj;
struct BitMapHeader *bmh;
struct BitMap *bmap;
struct gpLayout layout;
ULONG *creg, *ctab;
UBYTE *str;
LONG ncol, crsize, err;
memset(pic, 0, sizeof(struct Picture)); /* clear pic structure */
err = 0;
if (obj = NewDTObject(file_name,
DTA_SourceType, DTST_FILE,
DTA_GroupID, GID_PICTURE,
PDTA_Remap, FALSE,
TAG_DONE)) /* get the picture object */
{
if (GetDTAttrs(obj,
PDTA_ModeID, &pic->display_ID,
PDTA_BitMapHeader, &bmh,
TAG_DONE) == 2) /* get the bitmap_header and mode_id */
{
pic->bmhd = *bmh;
/*
query the object about its author, copyright and annotation
*/
if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
{
if (str)
{
pic->author_size = strlen(str) + 1;
if (pic->author = AllocMem(pic->author_size, 0))
strcpy(pic->author, str);
}
}
if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
{
if (str)
{
pic->copyright_size = strlen(str) + 1;
if (pic->copyright = AllocMem(pic->copyright_size, 0))
strcpy(pic->copyright, str);
}
}
if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
{
if (str)
{
pic->annotation_size = strlen(str) + 1;
if (pic->annotation = AllocMem(pic->annotation_size, 0))
strcpy(pic->annotation, str);
}
}
layout.MethodID = DTM_PROCLAYOUT; /* render the object */
layout.gpl_GInfo = NULL;
layout.gpl_Initial = TRUE;
if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
{
if (GetDTAttrs(obj,
PDTA_BitMap, &bmap,
PDTA_CRegs, &creg,
PDTA_NumColors, &ncol,
TAG_DONE) == 3) /* get the bitmap and its colors */
{
if (bmap != NULL && creg != NULL && ncol != 0)
{
crsize = (ncol * 3) * 4;
pic->palette_entries = ncol;
pic->palette_size = crsize + (2 * 4); /* LoadRGB32() table requirements */
if (pic->palette = AllocMem(pic->palette_size, 0))
{
ctab = pic->palette;
*ctab++ = (ncol << 16) | 0; /* number of colors and first color to load */
memcpy(ctab, creg, crsize);
*(ctab + (crsize / 4)) = 0; /* terminator */
}
else err = ERROR_NO_FREE_STORE;
if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, bmap))
{
BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
WaitBlit();
}
else err = ERROR_NO_FREE_STORE;
}
else err = ERROR_REQUIRED_ARG_MISSING;
}
else err = IoErr();
}
else err = IoErr();
}
else err = ERROR_REQUIRED_ARG_MISSING;
DisposeDTObject(obj); /* free the object */
}
else err = IoErr();
if (err)
FreePicture(pic);
return(err);
}
/*
FreePicture
Parameters
pic: Picture structure with resources to free
Return value
none
*/
void FreePicture(struct Picture *pic)
{
if (pic->bmap)
{
WaitBlit();
FreeBitMap(pic->bmap);
}
if (pic->palette)
FreeMem(pic->palette, pic->palette_size);
if (pic->author)
FreeMem(pic->author, pic->author_size);
if (pic->copyright)
FreeMem(pic->copyright, pic->copyright_size);
if (pic->annotation)
FreeMem(pic->annotation, pic->annotation_size);
memset(pic, 0, sizeof(struct Picture)); /* clear it all */
}
/*
ViewPicture
Parameters
pic: picture infos and data
Return value
TRUE if the user cancelled the view sequence (<Esc> key)
FALSE otherwise
*/
BOOL ViewPicture(struct Picture *pic)
{
struct Screen *scr;
struct Window *win;
struct IntuiMessage *imsg;
BOOL done, quit;
done = quit = FALSE;
if (scr = OpenScreenTags(NULL,
SA_Width, pic->bmhd.bmh_Width,
SA_Height, pic->bmhd.bmh_Height,
SA_Depth, pic->bmhd.bmh_Depth,
SA_Quiet, TRUE,
SA_ShowTitle, FALSE, /* no title bar */
SA_Behind, TRUE,
SA_Type, CUSTOMSCREEN,
SA_DisplayID, pic->display_ID,
SA_Overscan, OSCAN_TEXT,
SA_AutoScroll, TRUE,
SA_Colors32, pic->palette,
SA_BackFill, LAYERS_NOBACKFILL, /* no screen-clearing when the window is closed (is faster) */
TAG_END))
{
if (win = OpenWindowTags(NULL,
WA_Width, scr->Width,
WA_Height, scr->Height,
WA_IDCMP, MOUSEBUTTONS | VANILLAKEY,
WA_CustomScreen, scr,
WA_Backdrop, TRUE,
WA_Borderless, TRUE,
WA_Activate, TRUE,
WA_RMBTrap, TRUE,
WA_SimpleRefresh, TRUE,
WA_BackFill, LAYERS_NOBACKFILL, /* no screen-clearing when the window is opened (is faster) */
TAG_END))
{
BltBitMap(pic->bmap, 0,0, scr->RastPort.BitMap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
WaitBlit();
ScreenToFront(scr); /* show the screen only when the picture has been copied to it */
while (!done)
{
Wait(1 << win->UserPort->mp_SigBit);
while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
{
switch (imsg->Class)
{
case VANILLAKEY:
switch (imsg->Code)
{
case 27: done = quit = TRUE; break; /* <Esc> = cancel */
case 13:
case 32: done = TRUE; break; /* <Enter> / <Space> = continue */
}
break;
case MOUSEBUTTONS:
switch (imsg->Code)
{
case SELECTUP: /* MButton = continue */
case MIDDLEUP:
case MENUUP: done = TRUE; break;
}
break;
}
ReplyMsg((struct Message *)imsg);
}
}
CloseWindow(win);
}
CloseScreen(scr);
}
return(quit);
}
void main(int argc, char *argv[])
{
#define AP_BUFFSIZE 240
#define PT_SIZE 80
struct Picture pic;
struct AnchorPath *ap;
UBYTE pic_type[PT_SIZE];
LONG err;
BOOL quit;
if (argc > 1)
{
if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39))
{
if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
{
if (DataTypesBase = OpenLibrary("datatypes.library", 39))
{
if (ap = AllocMem(sizeof(struct AnchorPath) + AP_BUFFSIZE, MEMF_CLEAR))
{
ap->ap_Strlen = AP_BUFFSIZE;
for (err = MatchFirst(argv[1], ap); err == 0; err = MatchNext(ap))
{
if (IsDataTypes(ap->ap_Buf, pic_type, PT_SIZE))
{
if (GetDataTypesPicture(ap->ap_Buf, &pic, 0) == 0)
{
printf("%s (%s, %dx%d, %d colors)\n",
ap->ap_Info.fib_FileName,
pic_type,
pic.bmhd.bmh_Width,
pic.bmhd.bmh_Height,
pic.palette_entries);
if (pic.author)
printf(" author: %s\n", pic.author);
if (pic.copyright)
printf(" copyright: %s\n", pic.copyright);
if (pic.annotation)
printf(" annotation: %s\n", pic.annotation);
quit = ViewPicture(&pic);
FreePicture(&pic);
if (quit)
break;
}
}
}
MatchEnd(ap);
FreeMem(ap, sizeof(struct AnchorPath) + AP_BUFFSIZE);
}
CloseLibrary(DataTypesBase);
}
CloseLibrary((struct Library *)GfxBase);
}
CloseLibrary((struct Library *)IntuitionBase);
}
}
}